package com.financing.platform.activities.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.financing.platform.activities.ActivitiesBusinessObject;
import com.financing.platform.dao.IActivitesDao;
import com.financing.platform.dao.IActivitieGiftDao;
import com.financing.platform.dao.IFaUserDao;
import com.financing.platform.dao.IUserAccountDao;
import com.financing.platform.dto.ActivitesDTO;
import com.financing.platform.dto.ActivitieGiftDTO;
import com.financing.platform.dto.FaUserDTO;
import com.financing.platform.dto.UserAccountDetailDTO;
import com.financing.platform.util.Dict;

@Service("activitiesSDHDImpl")
public class ActivitiesSDHDImpl extends ActivitiesBusinessObject{

	protected Logger log =Logger.getLogger(this.getClass());
	
	@Autowired
	private IActivitieGiftDao activitieGiftDaoImpl;
	
	@Autowired
	private IActivitesDao activitesDao;
	
	@Autowired
	private IUserAccountDao userAccountDaoImpl;
	
	@Autowired
	private IFaUserDao faUserDaoImpl;
	
	@Override
	public void handleProductFound(Object object) throws Exception {
	}

	@Override
	public void handleFaUserRegister(Object object)  throws Exception{
		
	}

	@Override
	public void handleFaUserAuth(Object object) throws Exception {
		 @SuppressWarnings("unchecked")
		Map<String,Object> param = (Map<String,Object>) object;
		FaUserDTO  faUserDTO = (FaUserDTO) param.get("faUserDTO");
		if(!"1".equals(faUserDTO.getuRole()) &&  !"99".equals(faUserDTO.getuRole()) && !"98".equals(faUserDTO.getuRole())){
			FaUserDTO userDTO = new FaUserDTO();
			userDTO.setId(faUserDTO.getId());
			userDTO = faUserDaoImpl.getFaUser(userDTO);
			ActivitieGiftDTO gift = new ActivitieGiftDTO();
			
			//打开自己的冻结金额
			gift.setFaUserId(userDTO.getId());
			List<ActivitieGiftDTO> list = activitieGiftDaoImpl.queryActivitiesSDHDGift(gift);
			for(ActivitieGiftDTO tmp : list){
				if("02".equals(tmp.getaStatus() )&& (new BigDecimal(2).equals(tmp.getaAmount())||new BigDecimal(4).equals(tmp.getaAmount())|| new BigDecimal(6).equals(tmp.getaAmount())||new BigDecimal(8).equals(tmp.getaAmount()))){
					
					//解冻红包金额
					userAccountDaoImpl.thawingAccountMount(tmp);
					
					 //更新活动已发放总金额
					 ActivitesDTO dto = new ActivitesDTO();
					 dto.setActiviesNowAmount(tmp.getaAmount());
					 dto.setActiviesCode("SDHD");
					 activitesDao.updateAcitivitesNowAmount(dto);
					 break;
				}
			}
			
			//打开分享人的一个红包
			gift.setFaUserId(userDTO.getSourceCode());
			 list = activitieGiftDaoImpl.queryActivitiesSDHDGift(gift);
			
				for(ActivitieGiftDTO tmp : list){
					if("01".equals(tmp.getaStatus())){
						tmp.setUpdateBy(faUserDTO.getUpdateBy());
						tmp.setUpdateDate(faUserDTO.getUpdateDate());
						userAccountDaoImpl.addAccountMount(tmp);
						
						UserAccountDetailDTO userAccountDetailDTO = new UserAccountDetailDTO();
						
						userAccountDetailDTO.setAccountId(userAccountDaoImpl.getUserAccountId(tmp.getFaUserId()));
						userAccountDetailDTO.setAmount(tmp.getaAmount());
						userAccountDetailDTO.setActivitieGiftId(tmp.getId());
						userAccountDetailDTO.setFaId(tmp.getFaUserId());
						userAccountDetailDTO.setAccountType(Dict.P_ACCOUNT_DETAIL_TYPE_INPUT);
						userAccountDetailDTO.setOperateType(Dict.P_ACCOUNT_OPERATE_ACTIVITY);
						userAccountDetailDTO.setOperateStatus(Dict.P_ACCOUNT_OPERATE_STATUS_SUCCESS);
						userAccountDetailDTO.setUpdateBy(faUserDTO.getUpdateBy());
						userAccountDetailDTO.setUpdateDate(faUserDTO.getUpdateDate());
						userAccountDetailDTO.setCreateBy(faUserDTO.getUpdateBy());
						userAccountDetailDTO.setCreateDate(faUserDTO.getUpdateDate());
						userAccountDetailDTO.setRemark("红包解锁");
						userAccountDaoImpl.insertUserAccountDetail(userAccountDetailDTO);
						 
						tmp.setaStatus("02");
						tmp.setUpdateBy(faUserDTO.getUpdateBy());
						tmp.setUpdateDate(faUserDTO.getUpdateDate());
						tmp.setFaShareUserId(userDTO.getId());
						 activitieGiftDaoImpl.updateActivitieGift(tmp);
						 
						 //更新活动已发放总金额
						 ActivitesDTO dto = new ActivitesDTO();
						 dto.setActiviesNowAmount(tmp.getaAmount());
						 dto.setActiviesCode("SDHD");
						 activitesDao.updateAcitivitesNowAmount(dto);
						 break;
					}
			}
		}
	}

	@Override
	public void handleOrderRebate(Object object) throws Exception {
		
	}
	
}
